import 'package:flutter/material.dart';

import '../common/flatPanel.dart';
import '../controller/topicItemController.dart';
import '../utils/screw.dart';

class TopicItemPage extends StatefulWidget {
  TopicItemPage(
      {Key key,
      @required this.title,
      @required this.table,
      @required this.configName})
      : super(key: key);

  final String title;
  final String table;
  final String configName;

  @override
  _TopicItemPageState createState() => _TopicItemPageState();
}

class _TopicItemPageState extends State<TopicItemPage>
    with TopicItemController {
  @override
  void initState() {
    super.initState();
    mTable = widget.table;
  }

  @override
  void dispose() {
    super.dispose();
    disposeAction();
  }

  @override
  Future<void> onConnectDB() async {
    await connectDbAction(context, widget.configName, onUpdateTopics);
  }

  @override
  Future<void> onUpdateTopics() async {
    await updateTopicsAction();
    setState(() {
      mViewState = ViewState.DetailView;
    });
  }

  @override
  Future<void> onUpdateView() async {
    await updateViewAction();
    setState(() {
      mViewState = ViewState.DetailView;
    });
  }

  @override
  Future<void> onAddItem() async {
    await addItemAction(context);
  }

  @override
  Future<void> onEditItem() async {
    await editItemAction(context);
  }

  @override
  Future<void> onDeleteItem() async {
    await deleteItemAction();
  }

  @override
  Future<void> onTapItem() async {
    if (mItem.id == placeHolder) {
      if (mItem.detail.isNotEmpty && mItem.detail.startsWith('http'))
        await Screw.openUrl(mItem.detail);
      placeHolder = -1;
      mViewState = ViewState.DetailView;
    } else {
      placeHolder = mItem.id;
      mViewState = ViewState.CheckedView;
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return genScaffoldAction(context, widget.title);
  }
}
